package resource.fileOperation;

import java.io.*;

/**
 * 文件管理，增删查改
 */
public class FileOp {
    //创建文件夹,多级目录
    public static boolean createFolder(String path){
        if (path == null || path.equals("")){
            System.out.println("路径为空");
            return false;
        }else{
            File file = new File(path);
            if (file.exists()){
                System.out.println("文件夹已经存在");
                return false;
            }else{
                System.out.println("文件夹创建成功");
                return file.mkdirs();
            }
        }
    }
    //删除文件夹
    public static boolean deleteFolder(String path){
        if (path == null || path.isEmpty()){
            System.out.println("文件路径为空");
            return false;
        }else {
            File file = new File(path);
            if (!file.exists()){
                System.out.println("文件不存在");
                return false;
            }else{
                return file.delete();
            }
        }
    }
    //修改文件夹名称
    public static boolean changeFileName(String path,String newName){
        if (path == null || path.isEmpty()){
            System.out.println("没有找到文件");
            return false;
        }else if(newName == null || newName.isEmpty()){
            System.out.println("文件新名字为空");
            return false;
        }else{
            File file = new File(path);
            String name = file.getParent();
            File newFile = new File(name+"//"+newName);
            return file.renameTo(newFile);
        }
    }
    //创建文件
    public static boolean createFile(String path,String fileName) throws IOException {
        if(path == null || path.isEmpty()){
            System.out.println("路径为空");
            return false;
        }else if(fileName == null || fileName.isEmpty()){
            System.out.println("文件名字为空");
            return false;
        }else{
            File file = new File(path+"//"+fileName);
            if (file.exists()){
                System.out.println("文件已经存在");
                return false;
            }else {
                return file.createNewFile();
            }
        }
    }
    //txt文件中写入
    public static boolean writeTxt(String path,String content) throws IOException {
        File file = new File(path);
        if (!file.exists()){
            System.out.println("文件不存在");
            return false;
        }else{
            BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
            writer.write("\n"+content);
            writer.close();
            return true;
        }
    }
    //读取txt文件中的内容
    //清空txt文件中的内容



}
